package com.example.bailin.giftsay.sort;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.entity.SingleEntity;
import com.example.bailin.giftsay.sort.activity.DPSkipActivity;
import com.example.bailin.giftsay.sort.adapter.SingleLeftAdapter;
import com.example.bailin.giftsay.sort.adapter.SingleRightAdapter;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;
import com.example.bailin.giftsay.tool.UrlProvide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by bailin on 16/8/17.
 * <p>
 * 单品页所有内容
 */
public class SingleSortFragment extends BaseFragment {

    private ListView lvLeft;
    private ListView lvRight;
    private int lastPosition;

    @Override
    protected int setLayout() {
        return R.layout.fragment_singlesort;
    }

    @Override
    protected void initView() {
        lvLeft = bindView(R.id.lv_singles_left);
        lvRight = bindView(R.id.lv_singles_right);
        // 隐藏 listview滚动条的方法
        lvLeft.setScrollbarFadingEnabled(false);
        lvRight.setScrollbarFadingEnabled(false);
    }

    @Override
    protected void initData() {
        tool().getData(UrlProvide.GIFT_LISTVIEW, SingleEntity.class, new NetTool.NetInterface<SingleEntity>() {
            @Override
            public void onSuccess(SingleEntity singleEntity) {
                SingleLeftAdapter leftAdapter = new SingleLeftAdapter();
                leftAdapter.setEntity(singleEntity);
                lvLeft.setAdapter(leftAdapter);
                // 左侧listview点击监听
                lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // 调用下方的方法
                        updateLeftListview(position);
                        // 左侧listview点击右侧显示对应位置
                        lvRight.setSelection(Integer.parseInt("" + position));
                    }
                });
            }

            @Override
            public void onError(String errorMsg) {
            }
        });

        tool().getData(UrlProvide.GIFT_LISTVIEW, SingleEntity.class, new NetTool.NetInterface<SingleEntity>() {
            @Override
            public void onSuccess(final SingleEntity singleEntity) {
                SingleRightAdapter rightAdapter = new SingleRightAdapter();
                rightAdapter.setEntity(singleEntity);
                rightAdapter.setLmClickListener(new LmClickListener() {
                    @Override
                    public void onClick(int id, String title) {
                        Intent intent = new Intent(MyApp.getContext(), DPSkipActivity.class);
                        intent.putExtra("url", "" + id);
                        intent.putExtra("title", title);
                        startActivity(intent);
                    }
                });
                lvRight.setAdapter(rightAdapter);
                // 右侧listview滑动监听
                lvRight.setOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {
                    }

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                        // 调用下方的方法
                        updateLeftListview(firstVisibleItem);
                        // 右侧listview滑动左侧也会滑动
                        lvLeft.setSelection(firstVisibleItem);
                    }
                });
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

    private void updateLeftListview(int firstVisibleItem) {
        // 失去焦点
        TextView lasTextView = (TextView) lvLeft.findViewWithTag(lastPosition);
        if (lasTextView != null) {
            lasTextView.setTextColor(Color.BLACK);
            lasTextView.setBackgroundColor(0XFFF5F2F2);
        }

        // 获取焦点
        TextView currenTextView = (TextView) lvLeft.findViewWithTag(firstVisibleItem);
        if (currenTextView != null) {
            currenTextView.setTextColor(Color.RED);
            currenTextView.setBackgroundColor(0XFFFFFFFF);
        }
        lastPosition = firstVisibleItem;
    }

}